package vg.userInterface.scaling.components;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import vg.core.IGraphView;
/**
* This class realizes zoom in.
* @author tzolotuhin
*/
public class ZoomOut {
// Main components
private final JButton button;
// Data
private IGraphView view;
// Mutex
private final Object theMutexObject;
/**
* Constructor.
*/
public ZoomOut() {
// init mutex
this.theMutexObject = new Object();
// init components
this.button = new JButton(new ImageIcon("./data/resources/textures/minusButton.png"));
this.button.setToolTipText("Zoom out");
this.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
zoomOut();
}
});
this.button.setEnabled(false);
// ctrl + minus
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e) {
// if use VK_MINUS, that it's will not work, because JDK has bug with it.
if (e.getID() == KeyEvent.KEY_PRESSED && (e.getKeyChar() == '-' || e.getKeyCode() == KeyEvent.VK_MINUS) && e.isControlDown()) {
zoomOut();
return(true);
}
return(false);
}
});
}
public JComponent getView() {
return(this.button);
}
/**
* This method changes current view.
*/
public void changeView(final IGraphView newView) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
synchronized (ZoomOut.this.theMutexObject) {
ZoomOut.this.view = newView;
button.setEnabled(ZoomOut.this.view != null);
}
}
});
}
///////////////////////////////////////////////////////////////////////////
// PRIVATE METHOD
///////////////////////////////////////////////////////////////////////////
private void zoomOut() {
synchronized (this.theMutexObject) {
if(this.view!=null) {
this.view.zoomOut();
}
}
}
}